package com.wlps.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wlps.contant.Contant;
import com.wlps.entity.SecondClassification;
import com.wlps.mapper.FirstClassificationMapper;
import com.wlps.mapper.SecondClassificationMapper;
import com.wlps.result.ResultUtils;
import com.wlps.result.ResultVo;
import com.wlps.service.ISecondClassificationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wlps.vo.sendcenter.commodity.FirstClassificationVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * <p>
 * 二级分类表(second_classification)(删除二级分类时下面不能存在商品。) 服务实现类
 * </p>
 *
 * @author ZWYZY
 * @since 2020-07-31
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class SecondClassificationServiceImpl extends ServiceImpl<SecondClassificationMapper, SecondClassification> implements ISecondClassificationService {
    @Autowired
    FirstClassificationMapper firstClassificationMapper;

    @Override
    public ResultVo addSecondClassification(Long firstId, String secondName) {
        if (firstClassificationMapper.selectById(firstId) == null) {
            return ResultUtils.error("一级分类id不存在,请重新输入，亲", 70000);
        }
        if (baseMapper.selectList(new QueryWrapper<SecondClassification>().lambda().eq(SecondClassification::getFirstId, firstId).eq(SecondClassification::getSecondName, secondName)).size()!=0) {
            return ResultUtils.error("一级分类id:" + firstId + "下，已有二级分类名称：" + secondName, 70001);
        }
        SecondClassification secondClassification = new SecondClassification();
        secondClassification.setFirstId(firstId);
        secondClassification.setSecondName(secondName);
        baseMapper.insert(secondClassification);
        firstClassificationVoUpdateCache();
        return ResultUtils.success("操作成功");
    }

    @Override
    public ResultVo<List<SecondClassification>> getSecondClassificationList(Long id) {
        List<SecondClassification> secondClassificationList=baseMapper.selectList(new QueryWrapper<SecondClassification>().lambda().eq(SecondClassification::getFirstId,id));
        if(secondClassificationList.size()==0){
            return ResultUtils.error("当前一级分类下没有二级分类,亲",900001);
        }
        return new ResultVo<>("获取成功",200,secondClassificationList);
    }


    @Autowired
    RedisTemplate redisTemplate;
    /**
     * 更新缓存
     */
    @Async
    public void firstClassificationVoUpdateCache() {

        List<FirstClassificationVo> firstClassificationVoList = firstClassificationMapper.selectFirstClassificationCount();
        firstClassificationVoList.stream().forEach(x -> {
            firstClassificationMapper.selectSecondClassificationCount().stream().forEach(y -> {
                if (x.getFirstName().equals(y.getFirstName())) {
                    x.setSecondClassificationNum(y.getSecondClassificationNum());
                }
            });
            x.setSecondClassificationsList(firstClassificationMapper.selectSecondClassificationList().stream().filter(z -> z != null && z.getFirstId().equals(x.getId())).collect(Collectors.toList())
            );
        });

        redisTemplate.opsForValue().set(Contant.FIRSTCLASSIFICATIONVOS_KEY, firstClassificationVoList, 5, TimeUnit.MINUTES);
        log.info("更新所有一级分类-------存入缓存数据");

    }
}
